package com.itmk.business.images.service.Impl;

import com.itmk.business.article.entity.Article;
import com.itmk.business.images.mapper.QiniuMapper;
import com.itmk.business.images.service.QiniuService;
import com.itmk.config.qiniu.QiNiuConfig;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class QiniuServiceImpl implements QiniuService {

    @Autowired
    private QiNiuConfig qiNiuConfig;

    @Autowired
    private QiniuMapper qiniuMapper;

    private List<String> getImagesList(String bucketname) {
        List<String> imagelist=new ArrayList<>();
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
        String accessKey = qiNiuConfig.getAccesskey();
        String secretKey = qiNiuConfig.getSecretkey();

        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        //文件名前缀
        String prefix = "";
//每次迭代的长度限制，最大1000，推荐值 1000
        int limit = 1000;
//指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
        String delimiter = "";
//列举空间文件列表
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucketname, prefix, limit, delimiter);
        while (fileListIterator.hasNext()) {
            //处理获取的file list结果
            FileInfo[] items = fileListIterator.next();
            for (FileInfo item : items) {
                String imageString=item.key;
                imagelist.add(imageString);
            }
        }
        return imagelist;
    }


//    获取bucketname空间下的图片文件及对应的文章
    public List<Map<String, String>> getArticleByImagename(String bucketname){
        List<String> strings=getImagesList(bucketname);

        List<Map<String,String>> maplist=new ArrayList<>();
        for (String str:strings){
            Map<String,String> map=new HashMap<>();
            Article article=qiniuMapper.getArticleByImageKey(str);
            if(article!=null){
                map.put("title",article.getTitle());
            }else{
                map.put("title","");

            }
            map.put("bucketname",bucketname);
            map.put("imagekey",str);
            map.put("imageurl",qiNiuConfig.getDomain()+"/"+str);
            maplist.add(map);

        }
        return maplist;
    }

//    删除空间中的图片
    public Boolean deleteImageByKey(String key,String bucketname){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
        String accessKey = qiNiuConfig.getAccesskey();
        String secretKey = qiNiuConfig.getSecretkey();
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucketname, key);
            return true;
        } catch (QiniuException ex) {
            log.error("删除图片失败{}",ex.response.toString());
            return false;
        }
    }



}
